package com.weipai.education.controller;

import com.alibaba.fastjson.JSON;
import com.weipai.education.enums.RetCode;
import com.weipai.education.model.Role;
import com.weipai.education.model.api.Response;
import com.weipai.education.model.api.SaveRoleMenusRequest;
import com.weipai.education.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 15:25
 * 
 * 角色控制类
 */
@RestController
@Validated
public class RoleController {
    
    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @param status
     * @return
     */
    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    public Response list(@RequestParam(value = "status", required = false) String status) {
        return roleService.list(status);
    }

    /**
     * 角色添加--修改
     * @param role
     * @return
     */
    @RequestMapping(value = "/role/save", method = RequestMethod.POST)
    public Response save(Role role) {
        return roleService.save(role);
    }

    /**
     * 角色删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/role/deleteById", method = RequestMethod.GET)
    public Response deleteById(String id) {
        return roleService.deleteById(id);
    }

    /**
     * 更改角色状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/role/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(String id) {
        return roleService.changeStatus(id);
    }

    /**
     * 根据角色id获得权限列表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/role/getMenusByRoleId", method = RequestMethod.GET)
    public Response getMenusByRoleId(String  id) {
        return roleService.getMenusByRoleId(id);
    }

    /**
     * 根据角色id,保存 角色权限
     * @param data
     * @return
     */
    @RequestMapping(value = "/role/saveRoleMenus", method = RequestMethod.POST)
    public Response saveRoleMenus(@NotBlank(message = "传入数据不能为空") String data) { 
        return roleService.saveRoleMenus(data);
    }

    /**
     * 角色保存--修改
     * @param roleName
     * @param roleDesc
     * @param id
     * @return
     */
    @RequestMapping(value = "/role/saveRole", method = RequestMethod.POST)
    public Response saveRole(@NotBlank(message = "角色名称不能为空") String roleName,
                             @NotBlank(message = "角色描述不能为空") String roleDesc,
                             @RequestParam(value = "id", required = false) Integer id){
        return roleService.saveRole(roleName, roleDesc, id);
    }
    
}
